//
//  ViewController.m
//  demo
//
//  Created by Teplot_01 on 16/8/27.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "CalculationVC.h"
#import "ActivityListVC.h"
@interface CalculationVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation CalculationVC
{
    UICollectionView *_collectionView;
    UITableView *_tableView;
    
    //请求下来的字典
    NSDictionary *_responseDic;
    //请求下来的数组
    NSMutableArray *_responseArr;
    NSArray *_resArr;
    
    NSString *_activityIdStr;
    
    UILabel *_lb;
    UIButton *_btn;
    UIView *_topView;
}
- (void)viewWillAppear:(BOOL)animated{
    [_lb removeFromSuperview];
    [_btn removeFromSuperview];
    [self requsetForLeaderActityList];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = superBackgroundColor;
    [self initTableView];
    _responseArr = [NSMutableArray array];
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}
- (void)initTopView{
    _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, 45)];
    _topView.backgroundColor = superBackgroundColor;
    [self.view addSubview:_topView];
    UILabel *totalLb = [[UILabel alloc]initWithFrame:CGRectMake(30, 10, KScreenWidth-60, 35)];
    NSNumber *_totalMoney = _responseDic[@"totalMoney"];
    totalLb.text = [NSString stringWithFormat:@"%@%@%@",@"目前总支出：", _totalMoney,@"元"];
    totalLb.backgroundColor = Theme_Color;
    totalLb.textColor = White_Color;
    totalLb.font = [UIFont systemFontOfSize:14];
    totalLb.textAlignment = NSTextAlignmentCenter;
    //阴影效果
    totalLb.layer.shadowColor = [UIColor grayColor].CGColor;
    totalLb.layer.shadowOffset = CGSizeMake(2,2);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    totalLb.layer.shadowOpacity = 0.5;
    totalLb.layer.shadowRadius = 2;
    [_topView addSubview:totalLb];
}
//请求领队活动列表，把正在进行的活动的ID保存下来
- (void)requsetForLeaderActityList{
    //参数
    NSDictionary *parameters = @{
                                 @"clubId":[NSString readToNSDefaultsWithKey:kClubId],
                                 @"userId": [NSString readToNSDefaultsWithKey:kId],
                                 @"status": @"2",
                                 @"pageNo": @"1",
                                 @"pageSize": @"10",
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/activity/list.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSDictionary *_resActiListDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@<<<<>>>>",_resActiListDic);
        if ([_resActiListDic[@"code"] isEqualToString:@"3003"]) {
            _responseArr = [NSMutableArray arrayWithCapacity:0];
            [_tableView reloadData];
            [_topView removeFromSuperview];
            [_lb removeFromSuperview];
            [_btn removeFromSuperview];
            [self initRemindView];
            
        }else{
            NSNumber *activiId = _resActiListDic[@"list"][0][@"id"];
            _activityIdStr = activiId.stringValue;
            [self requestForBillList];
        }
     
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求活动列表失败"];
    }];
}
//
- (void)requestForBillList{
    //参数
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
    [parameters setValue:_activityIdStr forKey:@"activityID"];
//    [parameters setValue:[NSString readToNSDefaultsWithKey:kId] forKey:@"userID"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/activity/billList.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@><><><><>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        if ([_responseDic[@"code"] isEqualToString:@"3003"]||[_responseDic[@"code"] isEqualToString:@"3001"]) {
            [_lb removeFromSuperview];
            [_btn removeFromSuperview];
            [self initRemindView];
        }else{
            [self initTopView];
            [_tableView reloadData];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
    }];
}
- (void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(15, 45, KScreenWidth - 30, KScreenHeight -63+20)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = superBackgroundColor;
    [self.view addSubview:_tableView];
}
- (void)initRemindView{
    _lb = [[UILabel alloc]initWithFrame:CGRectMake(20, 180, KScreenWidth-40, 60)];
    _lb.numberOfLines = 2;
    _lb.textAlignment = NSTextAlignmentCenter;
    _lb.textColor = Gray_Color;
    _lb.font = [UIFont systemFontOfSize:15];
    _lb.text = @"啊哦～没有正在进行的活动或者该活动还没有账单哦～快来选择感兴趣的活动加入我们吧!";
    [self.view addSubview:_lb];
    _btn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth/2-60, 300, 120, 30)];
    [_btn setTitle:@"查看热门活动" forState:UIControlStateNormal];
    _btn.layer.borderWidth = 1;
    _btn.layer.borderColor = Theme_Color.CGColor;
    _btn.layer.cornerRadius = 5;
    [_btn setTitleColor:Theme_Color forState:UIControlStateNormal];
    _btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_btn addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn];
}
#pragma - mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _responseArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    _resArr = _responseArr[section][@"billList"];
    return _resArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_responseArr.count == 0) {
        return cell;
    }else{
        UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 15, 15)];
        imageV.image = [UIImage imageNamed:@"icon"];
        [cell.contentView addSubview:imageV];
        UILabel *nameLb = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, 100, 35)];
        nameLb.text = _responseArr[indexPath.section][@"billList"][indexPath.row][@"payContent"];
        nameLb.font = [UIFont systemFontOfSize:15];
        [cell.contentView addSubview:nameLb];
        
        UILabel *payUserLb = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth/2-65, 0, 100, 35)];
        payUserLb.text = _responseArr[indexPath.section][@"billList"][indexPath.row][@"payerName"];
        payUserLb.textAlignment = NSTextAlignmentCenter;
        payUserLb.font = [UIFont systemFontOfSize:15];
        [cell.contentView addSubview:payUserLb];
        
        UILabel *spendLb = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth - 140, 0, 100, 35)];
        NSNumber *money = _responseArr[indexPath.section][@"billList"][indexPath.row][@"money"];
        NSString *moneyStr = money.stringValue;
        spendLb.text = [@"-" stringByAppendingString:moneyStr];
        spendLb.textAlignment = NSTextAlignmentRight;
        spendLb.font = [UIFont systemFontOfSize:15];
        [cell.contentView addSubview:spendLb];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, KScreenWidth, 30)];
    if (_responseArr.count == 0) {
        return customView;
    }else{
        customView.backgroundColor = superBackgroundColor;
        UILabel *timeLb = [[UILabel alloc]initWithFrame:CGRectMake(8, 0, 100, 30)];
        timeLb.text = _responseDic[@"list"][section][@"payTime"];
        timeLb.textColor = Gray_Color;
        timeLb.font = [UIFont systemFontOfSize:13];
        [customView addSubview:timeLb];
        UILabel *nameLb = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth-108-30, 0, 100, 30)];
        nameLb.text = @"悉尼探索";
        nameLb.textAlignment = NSTextAlignmentRight;
        nameLb.textColor = Gray_Color;
        nameLb.font = [UIFont systemFontOfSize:13];
//        [customView addSubview:nameLb];
        return customView;
    }
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, KScreenWidth, 20)];
    customView.backgroundColor = White_Color;
    UILabel *totayTotalLb = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth - 238, 0, 200, 30)];
    NSNumber *num = _responseArr[section][@"money"];
    NSString *todayMonStr = [NSString stringWithFormat:@"%@%@",@"今日总额:－",num.stringValue];
    totayTotalLb.text = todayMonStr;
    totayTotalLb.textAlignment = NSTextAlignmentRight;
    totayTotalLb.font = [UIFont systemFontOfSize:12];
    [customView addSubview:totayTotalLb];
    return customView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30;
}
- (void)clickAction{
    ActivityListVC *vc = [[ActivityListVC alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.status = @"1";
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
